<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/22
 * Time: 13:38
 */
namespace app\api\controller;
use think\Db;
use think\Request;
use app\api\model\Search as SearchModel;
use app\api\model\Tour;
use app\api\model\Hotel;
use app\api\model\Food;
use app\api\model\GroupTour;
use app\api\model\Method;
class Search extends Common
{
    /*
     * 搜索页面
     */
    public function search()
    {
        $post = input('param.');
        //历史搜索
        $select = new SearchModel();
        $data['history'] = $select->history($post['uid']);
        //推荐搜索
        $data['t_select'] = $select->t_search();

        return $this->apiReturn('200', '成功', $data);
    }

    /*
     * 搜索结果页
     */
    public function search_finish()
    {
        $post = input('param.');
        $type = input('type', '1', 'trim');
        $page = $this->p();
        $search = new SearchModel();
        $search->search_data($post['uid'], $post['title']);
        if ($type == 1) {
            $tour = new Tour();
            $where['tour_name'] = array('like', '%' . $post['title'] . '%');
            $tours = $tour->tour_list($where, $page, 3);
            return $this->apiReturn('200', '成功', $tours);
        } else if ($type == 2) {
            $hotel = new Hotel();
            $where['hotel_name'] = array('like', '%' . $post['title'] . '%');
            $hotels = $hotel->hotel_list($where, $page, '', $post['longitude'], $post['latitude']);
            return $this->apiReturn('200', '成功', $hotels);
        } else if ($type == 3) {
            $food = new Food();
            $where['food_store_name'] = array('like', '%' . $post['title'] . '%');
            $foods = $food->food_list($where, $page, '', $post['longitude'], $post['latitude']);
            return $this->apiReturn('200', '成功', $foods);
        } else if ($type == 5) {
            $group = new GroupTour();
            $where['gtour_title'] = array('like', '%' . $post['title'] . '%');
            $groups = $group->GetGroupTourList($where, $page);
            return $this->apiReturn('200', '成功', $groups);
        } else if ($type == 4) {
            $method = new Method();
            $where['title'] = array('like', '%' . $post['title'] . '%');
            $methods = $method->method_list($where,$page);
            $this->apiReturn('200', '成功', $methods);
        }
    }
    /*
     * 删除历史搜索
     */
    public function history_delete()
    {
        $post = input('param.');
        $search = new SearchModel();
        $del = $search->del_history($post['uid']);
        if($del){
            return $this->apiReturn('200', '删除成功');
        }else{
            return $this->apiReturn('400', '删除失败');
        }
    }
}